﻿using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
using PTop.Application.Questionnaire;
using PTop.Code;
using PTop.Code.Excel;
using PTop.Domain.Entity.Questionnaire;
using PTop.Web.Areas.Questionnaire.Models;
using System.Linq;

namespace PTop.Web.Areas.Questionnaire.Controllers
{
    /// <summary>
    /// 调查问卷试题Controller
    /// </summary>	
    public class QnQuestionController : ControllerBase
    {
        private QnQuestionApp _app = new QnQuestionApp();

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string pid, string keyword)
        {
            var data = _app.GetList(pagination, pid, keyword);
            return PageContent(pagination.records, pagination.rows, data);
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = _app.GetForm(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(QnQuestionEntity entity, string keyValue)
        {
            _app.SubmitForm(entity, keyValue);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            _app.Delete(keyValue);
            return Success("删除成功。");
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetStatisticsJson(string pid)
        {
            var questions = _app.GetListByPaper(pid);
            var answers = new QnAnswerApp().GetListByPaper(pid);
            var list = new List<QnStatisticsModel>();
            foreach (var question in questions)
            {
                var questAnswers = answers.Where(m => m.F_QuestionId == question.F_Id).ToList();
                var model = new QnStatisticsModel();
                model.F_QuestionId = question.F_Id;
                model.F_PaperTitle = question.F_PaperTitle;
                model.F_Question = question.F_Question;
                model.F_Type = question.F_Type;
                model.F_OrderId = question.F_OrderId;
                model.F_AnswerCont = questAnswers.Count;
                model.F_OptionCont = question.F_OptionCont;
                model.F_Option1 = questAnswers.Count(m => m.F_Option1);
                model.F_Option2 = questAnswers.Count(m => m.F_Option2);
                model.F_Option3 = questAnswers.Count(m => m.F_Option3);
                model.F_Option4 = questAnswers.Count(m => m.F_Option4);
                model.F_Option5 = questAnswers.Count(m => m.F_Option5);
                model.F_Option6 = questAnswers.Count(m => m.F_Option6);
                model.F_Option7 = questAnswers.Count(m => m.F_Option7);
                model.F_Option8 = questAnswers.Count(m => m.F_Option8);
                model.F_Option9 = questAnswers.Count(m => m.F_Option9);
                model.F_Option10 = questAnswers.Count(m => m.F_Option10);
                list.Add(model);
            }
            return Content(list.ToJson());
        }
        [HttpGet]
        [HandlerAuthorize]
        public virtual ActionResult StatisticsDetails(string keyValue, int type)
        {
            var question = _app.GetForm(keyValue);
            var questAnswers = new QnAnswerApp().GetListByQuestion(keyValue);
            var model = new QnStatisticsModel
            {
                QnQuestion = question,
                F_PaperTitle = question.F_PaperTitle,
                F_Question = question.F_Question,
                F_Type = question.F_Type,
                F_OrderId = question.F_OrderId,
                F_AnswerCont = questAnswers.Count,
                F_OptionCont = question.F_OptionCont,
                F_Option1 = questAnswers.Count(m => m.F_Option1),
                F_Option2 = questAnswers.Count(m => m.F_Option2),
                F_Option3 = questAnswers.Count(m => m.F_Option3),
                F_Option4 = questAnswers.Count(m => m.F_Option4),
                F_Option5 = questAnswers.Count(m => m.F_Option5),
                F_Option6 = questAnswers.Count(m => m.F_Option6),
                F_Option7 = questAnswers.Count(m => m.F_Option7),
                F_Option8 = questAnswers.Count(m => m.F_Option8),
                F_Option9 = questAnswers.Count(m => m.F_Option9),
                F_Option10 = questAnswers.Count(m => m.F_Option10)
            };
            if (type == 1)
            {
                return View("StatisticsBar", model);
            }
            else if (type == 2)
            {
                return View("StatisticsDashboard", model);
            }
            return new EmptyResult();
        }
        /// <summary>
        /// 下载统计结果
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public ActionResult DownloadStatistics(string pid)
        {
            var questions = _app.GetListByPaper(pid);
            var answers = new QnAnswerApp().GetListByPaper(pid);
            var list = new List<QnStatisticsModel>();
            foreach (var question in questions)
            {
                var questAnswers = answers.Where(m => m.F_QuestionId == question.F_Id).ToList();
                var model = new QnStatisticsModel();
                model.F_PaperTitle = question.F_PaperTitle;
                model.F_Question = question.F_Question;
                model.F_Type = question.F_Type;
                model.F_OrderId = question.F_OrderId;
                model.F_AnswerCont = questAnswers.Count;
                model.F_OptionCont = question.F_OptionCont;
                model.F_Option1 = questAnswers.Count(m => m.F_Option1);
                model.F_Option2 = questAnswers.Count(m => m.F_Option2);
                model.F_Option3 = questAnswers.Count(m => m.F_Option3);
                model.F_Option4 = questAnswers.Count(m => m.F_Option4);
                model.F_Option5 = questAnswers.Count(m => m.F_Option5);
                model.F_Option6 = questAnswers.Count(m => m.F_Option6);
                model.F_Option7 = questAnswers.Count(m => m.F_Option7);
                model.F_Option8 = questAnswers.Count(m => m.F_Option8);
                model.F_Option9 = questAnswers.Count(m => m.F_Option9);
                model.F_Option10 = questAnswers.Count(m => m.F_Option10);
                list.Add(model);
            }
            var q = questions.First();
            new ExcelExportHelper<QnStatisticsModel>(list)
                .AddProperty(m => m.F_Question, "问题")
                .AddProperty(m => m.F_TypeName, "题型")
                .AddProperty(m => m.F_OrderId, "排序")
                .AddProperty(m => m.F_AnswerCont, "答题总数")
                .AddProperty(m => m.F_OptionCont, "选项数")
                .AddProperty(m => m.F_Option1, "选项1")
                .AddProperty(m => m.F_Option2, "选项2")
                .AddProperty(m => m.F_Option3, "选项3")
                .AddProperty(m => m.F_Option4, "选项4")
                .AddProperty(m => m.F_Option5, "选项5")
                .AddProperty(m => m.F_Option6, "选项6")
                .AddProperty(m => m.F_Option7, "选项7")
                .AddProperty(m => m.F_Option8, "选项8")
                .AddProperty(m => m.F_Option9, "选项9")
                .AddProperty(m => m.F_Option10, "选项10")
                .ToExcel("调查问卷统计-" + q.F_PaperTitle);
            return new EmptyResult();
        }
    }
}